package com.taoyibao.hongcanting.ui.restaurant.delegate;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.kymjs.frame.view.AppDelegate;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.model.GiftInfoModel;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 14:20
 */
public class ScreenFragmentDelegate extends AppDelegate implements AdapterView.OnItemSelectedListener {
    private Spinner mSpinner;
    private List<String> mTimeData;
    private ArrayAdapter mAdapter;
    private EditText mEtInput;
    private List<GiftInfoModel> mData;
    private GiftInfoModel mCurrentChooseGift;
    /**
     * 要支付的金额
     */
    private TextView mTvAmount;
    /**
     * 单价
     */
    private TextView mTvUnitPrice;

    @Override
    public int getRootLayoutId() {
        return R.layout.fragment_screen;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        mData = new ArrayList<>();
        mTimeData = new ArrayList<>();
        mSpinner = get(R.id.sp_screen_time);
        mSpinner.setOnItemSelectedListener(this);
        mSpinner.setAdapter(mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_screen_time, mTimeData));
        mEtInput = get(R.id.et_screen_input);
        mTvAmount = get(R.id.tv_screen_amount);
        mTvUnitPrice = get(R.id.tv_screen_unitPrice);
    }

    /**
     * 设置数据并刷新
     *
     * @param data
     */
    public void setData(List<GiftInfoModel> data) {
        if (data != null && data.size() > 0) {
            //更新源数据
            mData.clear();
            mData.addAll(data);
            //更新显示数据
            mTimeData.clear();
            for (int i = 0; i < mData.size(); i++) {
                mTimeData.add(mData.get(i).gift_name + "秒");
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 设置页面数据
     */
    public void setUIData(GiftInfoModel giftInfoModel) {
        mTvAmount.setText(CodeUtils.formatAmountUnit(giftInfoModel.price));
//        double unitPrice = giftInfoModel.price / Double.parseDouble(giftInfoModel.gift_name);
//        mTvUnitPrice.setText("每分钟" + CodeUtils.formatAmountUnit(unitPrice));
    }

    /**
     * 获取霸屏的信息
     *
     * @return
     */
    public String getContext() {
        if (TextUtils.isEmpty(mEtInput.getText().toString().trim())) {
            ToastUtils.showToast(CodeUtils.getIdString(R.string.map_inputContext));
            return "";
        }
        return mEtInput.getText().toString().trim();
    }

    /**
     * 获取选中的时间数据模型
     *
     * @return
     */
    public GiftInfoModel getChooseGift() {
        if (mCurrentChooseGift != null) {
            return mCurrentChooseGift;
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.map_chooseTime));
        return null;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mCurrentChooseGift = mData.get(position);
        setUIData(mCurrentChooseGift);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    /**
     * 设置输入内容为空
     */
    public void setEmptyInput() {
        mEtInput.setText("");
    }

}
